cmake_minimum_required(VERSION 2.8.3)
project(feynman_camera)

#编译前更新sdk
set(SDK_FEYNMAN_CAMERA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/sdk-feynman-camera)
#查找是否安装git
find_package(Git QUIET)
if(Git_FOUND)
  message(" git is found")
  message("sdk-feynman-camera path: " "${SDK_FEYNMAN_CAMERA_DIR}")
  option(GIT_SUBMODULE "Check submodules during build" ON)
  #更新git 子模块
  if(GIT_SUBMODULE)
    message(STATUS "Submodule update")
    #execute_process(
	    #      COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
	    #      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 
	    #      RESULT_VARIABLE GIT_SUBMOD_RESULT
	    #      OUTPUT_VARIABLE OUT_RESULT
	    #      ERROR_VARIABLE ERROR_RESULT)
    message(STATUS "Submodule update result: " ${GIT_SUBMOD_RESULT})
    message(STATUS "Submodule update output: " ${OUTPUT_RESULT})
    message(STATUS "Submodule update error: " ${ERROR_RESULT})
  endif()
else(Git_FOUND)
  message(FATAL_ERROR "git is not found, please install git")
endif()


find_package(catkin REQUIRED COMPONENTS
      camera_info_manager
      dynamic_reconfigure 
      sensor_msgs
      roscpp
      nodelet
      pcl_conversions
      pcl_ros
      image_transport
      message_generation
      rospy
      std_msgs
)
set(CMAKE_VERBOSE_MAKEFILE ON) 
#link_directories(/usr/lib/x86_64-linux-gnu)
#include_directories(/usr/include/yaml-cpp)

## Generate dynamic reconfigure parameters in the 'cfg' folder
generate_dynamic_reconfigure_options(
      cfg/resfps.cfg
)
   
#SET(CMAKE_CXX_FLAGS "-p -g")
SET(CMAKE_CXX_FLAGS "-std=c++14 -O3 -DUSEIMU")
SET(CMAKE_C_FLAGS "-O3 -DUSEIMU")
#MACHINE = $(shell uname -m)
execute_process(COMMAND uname -m  OUTPUT_VARIABLE  MACHINES )
execute_process(COMMAND getconf LONG_BIT  OUTPUT_VARIABLE  MACHINES_BIT )
MESSAGE(STATUS "NEXTVPU Machine : ${MACHINES}")
MESSAGE(STATUS "NEXTVPU Machine Bits : ${MACHINES_BIT}")
IF ( (${MACHINES} MATCHES "x86_64") AND (${MACHINES_BIT} MATCHES "64")  )
      set(HOST_PLATFORM  "x64")
# for schroot enviroment
ELSEIF ( (${MACHINES} MATCHES "x86_64") AND (${MACHINES_BIT} MATCHES "32")  )
      set(HOST_PLATFORM  "x86")
ELSEIF ( ${MACHINES} MATCHES "x86" )
ELSEIF ( ${MACHINES} MATCHES "x86" )
      set(HOST_PLATFORM  "x86")
ELSEIF ( ${MACHINES} MATCHES "i686" )
      set(HOST_PLATFORM  "x86")
ELSEIF ( ${MACHINES} MATCHES "i386" )
      set(HOST_PLATFORM  "x86")
ELSEIF ( ${MACHINES} MATCHES "arm" )
      set(HOST_PLATFORM  "arm")
ELSEIF ( (${MACHINES} MATCHES "aarch64") AND (${MACHINES_BIT} MATCHES "64")  )
      set(HOST_PLATFORM  "arm64")
ELSEIF ( (${MACHINES} MATCHES "aarch64") AND (${MACHINES_BIT} MATCHES "32")  )
      set(HOST_PLATFORM  "arm")
ENDIF ()

message(STATUS "NEXTVPU : ${HOST_PLATFORM}")

add_service_files(
  FILES
  SetManualExposure.srv
  SetAutoExposure.srv
  SetStreamMode.srv
  GetImuExternalRef.srv
  GetImuInternalRef.srv
  GetLEFTIRParam.srv
  GetRGBParam.srv
  GetSN.srv
  SetDepthMode.srv
  SetProjector.srv
  EnablePointCloud.srv
  EnableIMU.srv
  EnableLK.srv
  SwitchRectify.srv
  SaveDepth.srv
  UploadFile.srv
  RemoveLow.srv
  RemoveLight.srv
  RemoveBad.srv
  RemoveEdge.srv
  SetSysTime.srv
  GetRGBExposure.srv
  SetRGBExposure.srv
  GetVersions.srv
  GetExposure.srv
  GetDeviceList.srv
)

add_message_files(FILES
  temp_info.msg
  cnn_info.msg
  cnn_box.msg
  imu_frame.msg
  imu_info.msg
  h265_raw.msg
)

generate_messages(
  DEPENDENCIES
  std_msgs sensor_msgs
)

catkin_package(
  INCLUDE_DIRS sdk-feynman-camera/include
  LIBRARIES feynman_camera
  CATKIN_DEPENDS rospy std_msgs pcl_conversions pcl_msgs pcl_filters camera_info_manager dynamic_reconfigure  sensor_msgs roscpp message_runtime
  
)
EXEC_PROGRAM("grep VERSION_ID -r /etc/os-release |awk -F = '{print $2}'" OUTPUT_VARIABLE DISTRIB_VERSION_ID RETURN_VALUE LS_RVALUE)

message(STATUS "VERSION : ${DISTRIB_VERSION_ID}")
      IF ( ${DISTRIB_VERSION_ID} MATCHES "16.04" )
      set(PCLPATH  "/usr/include/pcl-1.7")
      ELSEIF ( ${DISTRIB_VERSION_ID} MATCHES "18.04"  )
      set(PCLPATH  "/usr/include/pcl-1.8")
      ELSEIF (${DISTRIB_VERSION_ID} MATCHES "20.04"   )
      set(PCLPATH  "/usr/include/pcl-1.10")
      ENDIF ()

      
add_subdirectory(sdk-feynman-camera)
add_executable(feynman_camera
   ros/feynman_camera_node.cpp
   ros/codec_ctx.cpp
)
add_library(feynman_nodelet
   ros/feynman_camera_nodelet.cpp
)

add_dependencies(feynman_camera ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
add_dependencies(feynman_nodelet ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

#pragma comment(lib,"opencv_core453d.lib")
#pragma comment(lib,"opencv_imgproc453d.lib")
#pragma comment(lib,"ippicvmt.lib")
#pragma comment(lib,"IlmImfd.lib")
#pragma comment(lib,"ippiwd.lib")
#pragma comment(lib,"ittnotifyd.lib")
#pragma comment(lib,"zlibd.lib")

include_directories(/usr/include/eigen3/Eigen include sdk-feynman-camera/include sdk-feynman-camera/include/cnn sdk-feynman-camera/include/libyuv /usr/include/opencv4 /usr/include/libusb-1.0 ${PCLPATH} /usr/include/eigen3
${catkin_INCLUDE_DIRS}
)
target_link_libraries(feynman_camera
  feynman
	#-Wl,-Bstatic
	opencv_imgproc
	#-Wl,-Bstatic
	opencv_imgcodecs
	#-Wl,-Bstatic
	opencv_core
	#-Wl,-Bdynamic
  avformat 
  avcodec 
  avutil
  swresample 
  swscale
	 ${catkin_LIBRARIES} usb-1.0 yaml tbb jsoncpp
	)#opencv_highgui)

target_link_libraries(feynman_nodelet
  feynman
	#-Wl,-Bstatic
	opencv_imgproc
	#-Wl,-Bstatic
	opencv_imgcodecs
	#-Wl,-Bstatic
	opencv_core
	#-Wl,-Bdynamic
  avformat 
  avcodec 
  avutil
  swresample 
  swscale
	 ${catkin_LIBRARIES} usb-1.0 yaml tbb jsoncpp
	)#opencv_highgui)
add_dependencies(feynman_camera ${PROJECT_NAME}_generate_messages_cpp)

install(TARGETS feynman_camera ${ADDITIONAL_EXECUTABLES}
  ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
  )

install(FILES nextvpu-usb.rules
        DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}/${PROJECT_NAME}
)

install(FILES nextvpu-usb.rules
        DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
)

install(DIRECTORY launch
        DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
)
